import os

from Base import GetYamlFile
from Base.ReadExcel import Read_Excel
from Base.WriteExcel import Write_excel

list2 = []
list3 = []
list4 = []
list1 = []
list5 = []
YmlList = GetYamlFile.getYamlFile()
TestCasePath = YmlList["fengxiancPath"]
AllCasePath = YmlList["allcPath"]
WritePath = YmlList["writecPath"]
WritePath2 = YmlList["writecPath2"]
gysPath = YmlList["gysPath"]
gysAllPath = YmlList["gysAllPath"]
WritegysPath = YmlList["writegysPath"]
carPath = YmlList["carPath"]
duibiPath = YmlList['duibiPath']
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))


def kesshang():
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, TestCasePath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    for i in range(1, num):
        name = readBook.read_Excel("sheet", i, 1)
        code = readBook.read_Excel("sheet", i, 0)
        d = {"name": name, "code": code}
        list1.append(d)
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, AllCasePath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num2 = readBook.get_RowNums("sheet")

    for i in range(1, num2):
        name = readBook.read_Excel("sheet", i, 0)
        code = readBook.read_Excel("sheet", i, 1)
        jc = readBook.read_Excel("sheet", i, 2)
        idCard = readBook.read_Excel("sheet", i, 4)
        fenlei = readBook.read_Excel("sheet", i, 5)
        zhujian = readBook.read_Excel("sheet", i, 6)
        d = {"name": name, "code": code, "jc": jc, "idCard": idCard, "fenlei": fenlei, "zhujian": zhujian}
        list2.append(d)
    for i in list1:
        # print(find_data(list2, i.get("code"), "code", "code"), type(find_data(list2, i.get("code"), "code", "code")))
        if isinstance(find_data(list2, i.get("code"), "code", "code"), str):
            list4.append(find_data(list2, i.get("code"), "code", "code"))
        elif isinstance(find_data(list2, i.get("code"), "code", "code"), dict):
            list3.append(find_data(list2, i.get("code"), "code", "code"))
    print(list4)
    print(list3)
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, WritePath)
    print(REPORT_XLSX_DIR)
    wt = Write_excel(REPORT_XLSX_DIR, "sheet")
    for i in range(0, len(list3)):
        wt.write(i + 2, 1, list3[i].get("name"))
        wt.write(i + 2, 2, list3[i].get("code"))
        wt.write(i + 2, 3, list3[i].get("jc"))
        wt.write(i + 2, 5, list3[i].get("idCard"))
        wt.write(i + 2, 6, list3[i].get("fenlei"))
        wt.write(i + 2, 7, list3[i].get("zhujian"))
        wt.write(i + 2, 14, "已审核")
        wt.write(i + 2, 15, "启用")
        wt.write(i + 2, 16, "是")


def gongyishang():
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, gysPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    print(num)
    for i in range(1, num):
        name = readBook.read_Excel("sheet", i, 1)
        d = {"name": name}
        list1.append(d)
    print(list1)
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, gysAllPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num2 = readBook.get_RowNums("sheet")
    print(num2)
    for i in range(1, num2):
        name = readBook.read_Excel("sheet", i, 2)
        code = readBook.read_Excel("sheet", i, 1)
        jc = readBook.read_Excel("sheet", i, 3)
        idCard = readBook.read_Excel("sheet", i, 5)
        fenlei = readBook.read_Excel("sheet", i, 4)
        zhujian = readBook.read_Excel("sheet", i, 11)
        d = {"name": name, "code": code, "jc": jc, "idCard": idCard, "fenlei": fenlei, "zhujian": zhujian}
        list2.append(d)
    for i in list1:
        print(find_data(list2, i.get("name")), type(find_data(list2, i.get("name"))))
        if isinstance(find_data(list2, i.get("name")), str):
            list4.append(find_data(list2, i.get("name")))
        elif isinstance(find_data(list2, i.get("name")), dict):
            list3.append(find_data(list2, i.get("name")))
    print("总表中没有供应商：", list4)
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, WritegysPath)
    wt = Write_excel(REPORT_XLSX_DIR, "1")
    for i in range(0, len(list3)):
        wt.write(i + 2, 1, list3[i].get("name"))
        wt.write(i + 2, 2, list3[i].get("code"))
        wt.write(i + 2, 3, list3[i].get("jc"))
        wt.write(i + 2, 5, list3[i].get("idCard"))
        wt.write(i + 2, 6, list3[i].get("fenlei"))
        wt.write(i + 2, 7, list3[i].get("zhujian"))
        wt.write(i + 2, 14, "已审核")
        wt.write(i + 2, 15, "启用")
        wt.write(i + 2, 16, "是")


def dui_bi():
    list6 = []
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, WritePath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    for i in range(1, num):
        name = readBook.read_Excel("sheet", i, 0)
        code = readBook.read_Excel("sheet", i, 1)
        jc = readBook.read_Excel("sheet", i, 2)
        idCard = readBook.read_Excel("sheet", i, 4)
        fenlei = readBook.read_Excel("sheet", i, 5)
        zhujian = readBook.read_Excel("sheet", i, 6)
        d = {"name": name, "code": code, "jc": jc, "idCard": idCard, "fenlei": fenlei, "zhujian": zhujian}
        list6.append(d)
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, duibiPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num2 = readBook.get_RowNums("sheet")
    list7 = []
    list8 = []
    list9 = []
    for i in range(1, num2):
        name = readBook.read_Excel("sheet", i, 0)
        code = readBook.read_Excel("sheet", i, 1)
        jc = readBook.read_Excel("sheet", i, 2)
        idCard = readBook.read_Excel("sheet", i, 4)
        fenlei = readBook.read_Excel("sheet", i, 5)
        zhujian = readBook.read_Excel("sheet", i, 6)
        d = {"name": name, "code": code, "jc": jc, "idCard": idCard, "fenlei": fenlei, "zhujian": zhujian}
        list7.append(d)
    for i in list6:
        # print(find_data(list7, i.get("name")), type(find_data(list7, i.get("name"))))
        if isinstance(find_data(list7, i.get("code"), "code", "code"), str):
            list8.append(find_data(list7, i.get("code"), "code", "code"))
        elif isinstance(find_data(list7, i.get("code"), "code", "code"), dict):
            list9.append(find_data(list7, i.get("code"), "code", "code"))
    print(list9)
    print(len(list9))
    for i in list9:
        target_key = "code"
        target_value = i.get("code")
        list6 = [d for d in list6 if d.get(target_key) != target_value]
    print(list6)
    print(len(list6))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, WritePath2)
    print(REPORT_XLSX_DIR)
    wt = Write_excel(REPORT_XLSX_DIR, "sheet")
    for i in range(0, len(list6)):
        wt.write(i + 2, 1, list6[i].get("name"))
        wt.write(i + 2, 2, list6[i].get("code"))
        wt.write(i + 2, 3, list6[i].get("jc"))
        wt.write(i + 2, 5, list6[i].get("idCard"))
        wt.write(i + 2, 6, list6[i].get("fenlei"))
        wt.write(i + 2, 7, list6[i].get("zhujian"))
        wt.write(i + 2, 14, "已审核")
        wt.write(i + 2, 15, "启用")
        wt.write(i + 2, 16, "是")


def find_data(data_list, target, value, value2):
    for item in data_list:
        if item.get(value) == target or item.get(value2) == target:
            return item  # 找到了目标数据，返回该数据
    return target


if __name__ == '__main__':
    kesshang()
